package com.awspaas.user.apps.cpq.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.BOCopyAPI;
import com.actionsoft.sdk.local.api.BOQueryAPI;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.cpq.util.CreateMK;

public class CPQ_GetNewProjectWeb {

	public String getNewProject(UserContext userContext,String type,String bindId) {
		try {
			//主表信息
			BOQueryAPI data=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG").bindId(bindId);
			//设备模块明细信息
			BOQueryAPI modules=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG_GRIDS").bindId(bindId);
			//参数信息明细
			BOQueryAPI params=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG_PARAMETER_GRIDS").bindId(bindId);
			//其他费用明细
			BOQueryAPI otherPrice=SDK.getBOAPI().query("BO_EU_CPQ_PRICE_OTHER").bindId(bindId);
			//直发物资运费明细
			//BOQueryAPI zfwzyf=SDK.getBOAPI().query("BO_EU_CPQ_PRICE_TRANSPORT").bindId(bindId);
			//单栋设备价格明细
			BOQueryAPI systems=SDK.getBOAPI().query("BO_EU_CPQ_SYSTEM_PRICE").bindId(bindId);
			//设备安装费用成本明细
			BOQueryAPI IF=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG_IF").bindId(bindId);
			//安装费用明细
			BOQueryAPI IFP=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG_IFP").bindId(bindId);
			//安装费用外包明细
			BOQueryAPI WIFP=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG_WIFP").bindId(bindId);
			//安装费用指导明细
			BOQueryAPI ZIFP=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG_ZIFP").bindId(bindId);

			//运输费明细
			BOQueryAPI YSF=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG_TP").bindId(bindId);
			//航运费明细
			BOQueryAPI HY=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG_SHIPPING_COST").bindId(bindId);
			//原材料重量明细
			BOQueryAPI ycl=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG_MATERIAL‌").bindId(bindId);
			//创建项目报价记录台账实例
			String pid=SDK.getProcessAPI().createBOProcessInstance("obj_2ce868263b6943c59628031d9943a7e9",userContext.getUID(), "").getId();
			BOCopyAPI newData=data.copyTo("BO_EU_CPQ_PROJECT_MSG", pid);
			if(type.equals("引用为模板")) {
				newData.addNewData("SFWMB", "是");
			}else {
				newData.addNewData("SFWMB", "否");
			}
			newData.removeMapping("APPLYNO");
			newData.removeMapping("APPLYDATE");
			newData.removeMapping("APPLYUSER");
			newData.removeMapping("APPLYDEPT");
			newData.removeMapping("APPLYUID");
			newData.removeMapping("APPLYDEPTID");
			newData.removeMapping("FLOWSTATUS");
			if(type.equals("二次报价")) {
				//定制明细
				BOQueryAPI DZ=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG_DZ").bindId(bindId);
				BOCopyAPI newDZ=DZ.copyTo("BO_EU_CPQ_PROJECT_MSG_DZ", pid);
				newDZ.exec();
				newData.addNewData("VISION", "");
			}else {
				newData.removeMapping("VISION");
			}
			newData.exec();

			BOCopyAPI newModules=modules.copyTo("BO_EU_CPQ_PROJECT_MSG_GRIDS", pid);
			newModules.exec();
			BOCopyAPI newParams=params.copyTo("BO_EU_CPQ_PROJECT_MSG_PARAMETER_GRIDS", pid);
			newParams.exec();
			BOCopyAPI newOther=otherPrice.copyTo("BO_EU_CPQ_PRICE_OTHER", pid);
			newOther.exec();
			//BOCopyAPI newZF=zfwzyf.copyTo("BO_EU_CPQ_PRICE_TRANSPORT", pid);
			//newZF.exec();
			BOCopyAPI newSystems=systems.copyTo("BO_EU_CPQ_SYSTEM_PRICE", pid);
			newSystems.exec();
			BOCopyAPI newIF=IF.copyTo("BO_EU_CPQ_PROJECT_MSG_IF", pid);
			newIF.exec();
			BOCopyAPI newIFP=IFP.copyTo("BO_EU_CPQ_PROJECT_MSG_IFP", pid);
			newIFP.exec();
			BOCopyAPI newWIFP=WIFP.copyTo("BO_EU_CPQ_PROJECT_MSG_WIFP", pid);
			newWIFP.exec();
			BOCopyAPI newZIFP=ZIFP.copyTo("BO_EU_CPQ_PROJECT_MSG_ZIFP", pid);
			newZIFP.exec();
			BOCopyAPI newYSF=YSF.copyTo("BO_EU_CPQ_PROJECT_MSG_TP", pid);
			newYSF.exec();
			BOCopyAPI newHY=HY.copyTo("BO_EU_CPQ_PROJECT_MSG_SHIPPING_COST", pid);
			newHY.exec();
			BOCopyAPI newYCL=ycl.copyTo("BO_EU_CPQ_PROJECT_MSG_MATERIAL‌", pid);
			newYCL.exec();
			BO datas=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG").bindId(pid).detail();
			String wfno=datas.getString("APPLYNO");
			if(type.equals("引用报价")) {

				createMKmsg(params,modules,userContext,pid,wfno);
			}else if (type.equals("二次报价")) {
				CreateMK mk=new CreateMK();
				mk.createMKSChange(params,modules,userContext,pid,bindId,wfno);
			}

			ResponseObject ro = ResponseObject.newOkResponse();
			Map<String, String> map=new HashMap<String, String>();
			map.put("bindId", pid);
			ro.setData(map);
			return ro.toString();
		} catch (Exception e) {
			throw  e;
		}

	}
	/**
	 * 引用报价，创建模块记录清单信息数量
	 * @param params
	 * @param modules
	 * @param userContext
	 * @param pid
	 */
	public static void createMKmsg(BOQueryAPI params,BOQueryAPI modules,UserContext userContext,String pid,String wfno) {
		try {
		List<BO> plists=	params.list();
		List<BO> mlists=	modules.list();
		Map<String, JSONObject> pMap = new HashMap<String, JSONObject>();
		Map<String, String> nameMap = new HashMap<String, String>();
		Map<String, String> noMap = new HashMap<>();
		if(plists.size()>0) {
			for (int i = 0; i < plists.size(); i++) {
				//JSONObject json = (JSONObject) list.get(i);
				BO json =  plists.get(i);
				String bh = json.getString("PARMENTIER_NO");
				String value =  json.getString("PARMENTIER_VALUE");
				String name =  json.getString("PARMENTIER_NAME");
				String dw =  json.getString("DW");
				JSONObject jsonObject=new JSONObject();
				//判断value是否为数据
				if( NumberUtils.isCreatable(value)) {
					jsonObject.put("type", "1");//值为1者表示value为数值
				}else {
					jsonObject.put("type", "0");//值为0者表示value为字符串
				}
				jsonObject.put("value", value);
				pMap.put(bh, jsonObject);
				nameMap.put(bh, name);
				int num = i + 1;
				noMap.put(bh, "B$" + num);
			}
			System.out.println("pMap:"+pMap.toString());
		}

		// 获取模块编码信息
				Map<String, BO> idMap = new HashMap<>();
				String bms = "";
				for (BO json : mlists) {
					//JSONObject json = (JSONObject) string;
					String bm = json.getString("MKBH");
					if (bms.isEmpty()) {
						bms = "'" + bm + "'";
					} else {
						bms += ",'" + bm + "'";
					}
					String id = json.getString("ID");
					idMap.put(id, json);
				}
				System.out.println("BMS:"+bms);
				String sql = "select  * from  BO_EU_CPQ_MODULE_MSG where   STATUS='启用' and MKBM in (" + bms + ")";
				List<RowMap> maps = DBSql.getMaps(sql);
				Map<String, RowMap> moduleMap = new HashMap<>();
				if (maps.size() > 0) {
					for (RowMap rowMap : maps) {
						String mkbm = rowMap.getString("MKBM");
						moduleMap.put(mkbm, rowMap);
					}
				}
				String wlsql = "select  m.MKBM,m.SL,m.WLJSGS,m.XH, w.BM,w.NAME,w.TYPE,w.GG,w.TH,w.DW,w.CLF,w.RGF,w.WXWG,w.CGF,w.DJ from BO_EU_CPQ_COMPONENT_PART w,(" +
						"select  a.MKBM,b.XH,b.WLMC,b.WLBM,b.LX,b.GG,b.DH,b.DW,b.SL,b.DJ,b.WLJSGS,b.CLF,b.RGF,b.WXWG,b.CGF"
						+ " from BO_EU_CPQ_MODULE_MSG_GRID  b left JOIN   BO_EU_CPQ_MODULE_MSG a on b.BINDID=a.BINDID "
						+ "where a.STATUS='启用' and a.MKBM in (" + bms + ")  ) m where w.BM=m.WLBM";
				String zfsql = "select  a.MKBM,b.* from BO_EU_BO_EU_CPQ_MODULE_MSG_STRAIGHT_HAIR   b left JOIN BO_EU_CPQ_MODULE_MSG a on b.BINDID=a.BINDID "
						+ "where   a.STATUS='启用' and a.MKBM in (" + bms + ")";
				String cssql = "select  a.MKBM,b.* from BO_EU_CPQ_MODULE_MSG_PRICE_PARAMETERS   b left JOIN BO_EU_CPQ_MODULE_MSG a on b.BINDID=a.BINDID "
						+ "where   a.STATUS='启用' and a.MKBM in (" + bms + ")";
				List<RowMap> wlmaps = DBSql.getMaps(wlsql);
				Map<String,List<RowMap>> wlmoduleMap = new HashMap<>();
				if (wlmaps.size() > 0) {
					for (RowMap rowMap : wlmaps) {
						String mkbm = rowMap.getString("MKBM");
						if(wlmoduleMap.containsKey(mkbm)) {
							List<RowMap> list=wlmoduleMap.get(mkbm);
							list.add(rowMap);
							wlmoduleMap.put(mkbm, list);
						}else {
							List<RowMap> list=new ArrayList<RowMap>();
							list.add(rowMap);
							wlmoduleMap.put(mkbm, list);
						}

					}
				}
				List<RowMap> zfmaps = DBSql.getMaps(zfsql);
				Map<String, List<RowMap>> zfmoduleMap = new HashMap<>();
				if (zfmaps.size() > 0) {
					for (RowMap rowMap : zfmaps) {
						String mkbm = rowMap.getString("MKBM");
						if(zfmoduleMap.containsKey(mkbm)) {
							List<RowMap> list=zfmoduleMap.get(mkbm);
							list.add(rowMap);
							zfmoduleMap.put(mkbm, list);
						}else {
							List<RowMap> list=new ArrayList<RowMap>();
							list.add(rowMap);
							zfmoduleMap.put(mkbm, list);
						}
					}
				}
				List<RowMap> csmaps = DBSql.getMaps(cssql);
				Map<String, List<RowMap>> csmoduleMap = new HashMap<>();
				if (csmaps.size() > 0) {
					for (RowMap rowMap : csmaps) {
						String mkbm = rowMap.getString("MKBM");
						if(csmoduleMap.containsKey(mkbm)) {
							List<RowMap> list=csmoduleMap.get(mkbm);
							list.add(rowMap);
							csmoduleMap.put(mkbm, list);
						}else {
							List<RowMap> list=new ArrayList<RowMap>();
							list.add(rowMap);
							csmoduleMap.put(mkbm, list);
						}
					}
				}
				//对模块数据中的物料进行计算
				Map<String, BO> mkMaps=new HashMap<>();
				Map<String, List<BO>> wlMaps=new HashMap<>();
				Map<String, List<BO>> zfMaps=new HashMap<>();
				Map<String, List<BO>> csMaps=new HashMap<>();
				for (String  id : idMap.keySet()) {
					Workbook workbookwl = new XSSFWorkbook();
					Workbook workbook = new XSSFWorkbook();
					try {
						BO json = idMap.get(id);
						String bm = json.getString("MKBH");
						System.out.println("模块名称："+json.getString("XDXH")+"，编码："+bm);
						// 创建公式评估器
						// 设置单元格值
						Sheet sheet = workbookwl.createSheet("Sheet1");
						for (int i = 0; i < plists.size(); i++) {
							int num = i + 1;
							Cell cellB1 = sheet.createRow(i).createCell(1);
							//JSONObject jsonObject = (JSONObject) list.get(i);
							BO jsonObject =  plists.get(i);
							String bh =  jsonObject.getString("PARMENTIER_NO");
							JSONObject jsonObject2=	pMap.get(bh);
							String lx=jsonObject2.getString("type");
							if(lx.equals("0")) {
								cellB1.setCellValue(jsonObject2.getString("value"));
								System.out.println("cellB1:"+cellB1.getStringCellValue());
							}else {
								String value=jsonObject2.getString("value");
								double  numValue=Double.parseDouble(value);
								 cellB1.setCellValue(numValue);
								 System.out.println("cellB1:"+cellB1.getNumericCellValue());
							}

						}
						//对参数数据进行创建
						List<BO> cslists=new ArrayList<>();
						if(csmoduleMap.containsKey(bm)) {
							List<RowMap> list=csmoduleMap.get(bm);
							if(list.size()>0) {
								for (RowMap rowMap2 : list) {
									BO cs=new BO();
									String no=rowMap2.getString("PARMENTIER_NO");
									JSONObject jsonObject2=	pMap.get(no);
									cs.set("PARMENTIER_NAME", rowMap2.getString("PARMENTIER_NAME"));
									cs.set("PARMENTIER_NO", no);
									cs.set("PARMENTIER_VALUE", jsonObject2!=null?jsonObject2.getString("value"):rowMap2.getString("PARMENTIER_VALUE"));
									cs.set("DW", rowMap2.getString("DW"));
									cs.set("BZ", rowMap2.getString("BZ"));
									cs.set("XH", rowMap2.getString("XH"));
									cslists.add(cs);
								}
								csMaps.put(bm, cslists);
							}
						}
						//判断模块中是否有物料数据
						List<BO> lists=new ArrayList<>();
						int size=0;
						if(wlmoduleMap.containsKey(bm)) {
						    	List<RowMap> materials=wlmoduleMap.get(bm);
						    	if(materials.size()>0) {
						    		size=materials.size();
						    		for (int i = 0; i < materials.size(); i++) {
						    			RowMap bo = materials.get(i);
						    			BO wlgrid=new BO();
						    			String price1 = bo.getString("DJ").isEmpty()?"0":bo.getString("DJ");
										String clf = bo.getString("CLF").isEmpty()?"0":bo.getString("CLF");
										String rgf = bo.getString("RGF").isEmpty()?"0":bo.getString("RGF");
										String wx = bo.getString("WXWG").isEmpty()?"0":bo.getString("WXWG");
										String cgf = bo.getString("CGF").isEmpty()?"0":bo.getString("CGF");
										String sl = bo.getString("SL").isEmpty()?"0":bo.getString("SL");
										//b.XH,b.WLMC,b.WLBM,b.LX,b.GG,b.DH,b.DW,b.SL,b.DJ,b.WLJSGS,b.CLF,b.RGF,b.WXWG,b.CGF
										wlgrid.set("XH",bo.getString("XH") );
										wlgrid.set("WLMC", bo.getString("NAME"));
										wlgrid.set("WLBM", bo.getString("BM"));
										wlgrid.set("LX", bo.getString("TYPE"));
										wlgrid.set("GG", bo.getString("GG"));
										wlgrid.set("DH", bo.getString("TH"));
										wlgrid.set("DW", bo.getString("DW"));
										wlgrid.set("DJ", price1);
										wlgrid.set("CLF", clf);
										wlgrid.set("RGF",rgf );
										wlgrid.set("WXWG",wx );
										wlgrid.set("CGF",cgf );
										wlgrid.set("WLJSGS",bo.getString("WLJSGS") );
										//w.BM,w.NAME,w.TYPE,w.GG,w.TH,w.DW
						    			String equation = bo.getString("WLJSGS");
										  Document document = Jsoup.parse(equation);
										  String newQuation=document.text();
										  if (!newQuation.equals("0")&&!newQuation.isEmpty()) {
											  System.out.println("参数计算公式："+newQuation);
												// 获取物料数量计算公式中参数代号，如果有则将代号存放在list中
												for (String key : pMap.keySet()) {
													int num = newQuation.indexOf(key);
													if (num > -1) {
														newQuation = newQuation.replace(key, noMap.get(key));
													}
												}
												System.out.println("转换后的参数计算公式："+newQuation);
												FormulaEvaluator formulaEvaluator = workbookwl.getCreationHelper().createFormulaEvaluator();
												Cell cellB1 = sheet.createRow(plists.size()+i).createCell(2);
												cellB1.setCellFormula(newQuation);
												formulaEvaluator.evaluateFormulaCell(cellB1);
												// 获取公式计算结果
												CellValue cellValue = formulaEvaluator.evaluate(cellB1);
												System.out.println("The result of the formula is: " + cellValue.getNumberValue());
												double total1 = Double.parseDouble(price1) * cellValue.getNumberValue();
												double clftotal1 = Double.parseDouble(clf) * cellValue.getNumberValue();
												double rgftotal = Double.parseDouble(rgf) * cellValue.getNumberValue();
												double wxtotal = Double.parseDouble(wx) * cellValue.getNumberValue();
												double cgftotal = Double.parseDouble(cgf) * cellValue.getNumberValue();
												System.out.println("物料名称:"+bo.getString("WLMC")+"单价："+price1+",总价："+total1);
												wlgrid.set("SL", cellValue.getNumberValue());
												wlgrid.set("ZJ",total1);
												wlgrid.set("ZCLF",clftotal1);
												wlgrid.set("ZGSF",rgftotal);
												wlgrid.set("ZWX",wxtotal);
												wlgrid.set("ZCGF",cgftotal);
										  }else {
											  double num = Double.parseDouble(sl);
											  double total1 = Double.parseDouble(price1) * num;
												double clftotal1 = Double.parseDouble(clf) * num;
												double rgftotal = Double.parseDouble(rgf) * num;
												double wxtotal = Double.parseDouble(wx) * num;
												double cgftotal = Double.parseDouble(cgf) * num;
												wlgrid.set("SL", sl);
												wlgrid.set("ZJ",total1);
												wlgrid.set("ZCLF",clftotal1);
												wlgrid.set("ZGSF",rgftotal);
												wlgrid.set("ZWX",wxtotal);
												wlgrid.set("ZCGF",cgftotal);

										  }
										  lists.add(wlgrid);
						    		}
						    	}
						}
						//对直发物资进行计算
						List<BO> zflist=new ArrayList<>();
						if(zfmoduleMap.containsKey(bm)) {
							List<RowMap> list=zfmoduleMap.get(bm);
							if(list.size()>0) {
								for (int i = 0; i < list.size(); i++) {
									RowMap rowMap =  list.get(i);
									BO zfgrid=new BO();
									String equation = rowMap.getString("JSGS");
									String yfdj=rowMap.getString("YFBZ").isEmpty()?"0":rowMap.getString("YFBZ");//运费单价；
								    double    price=Double.parseDouble(yfdj) ;
								    zfgrid.set("MC", rowMap.getString("MC"));
								    zfgrid.set("ZL", rowMap.getString("ZL"));
								    zfgrid.set("DW", rowMap.getString("DW"));
								    zfgrid.set("YFBZ", rowMap.getString("YFBZ"));
								    zfgrid.set("MDSF", rowMap.getString("MDSF"));
								    zfgrid.set("JSGS",rowMap.getString("JSGS") );
								    zfgrid.set("BZ", rowMap.getString("BZ"));
								    zfgrid.set("BM", rowMap.getString("BM"));
									Document document = Jsoup.parse(equation);
									String newQuation=document.text();
									if (!newQuation.equals("0")&&!newQuation.isEmpty()) {
										for (String key : pMap.keySet()) {
											int num = newQuation.indexOf(key);
											if (num > -1) {
												newQuation = newQuation.replace(key, noMap.get(key));
											}
										}
									// 计算出物料数量和当前模块的物料成本价格
										FormulaEvaluator formulaEvaluator = workbookwl.getCreationHelper().createFormulaEvaluator();
										Cell cellB1 = sheet.createRow(plists.size()+lists.size()+1+i).createCell(2);
										cellB1.setCellFormula(newQuation);
										formulaEvaluator.evaluateFormulaCell(cellB1);
										// 获取公式计算结果
										CellValue cellValue = formulaEvaluator.evaluate(cellB1);
										System.out.println("The result of the formula is: " + cellValue.getNumberValue());
										double zj=cellValue.getNumberValue()*price;
										zfgrid.set("YFJG",zj);
										zfgrid.set("SL",cellValue.getNumberValue());
									}else {
										String sl=rowMap.getString("SL").isEmpty()?"0":rowMap.getString("SL");//运费数量;
										 double num=Double.parseDouble(sl);
										 double zj=num*price;
										 zfgrid.set("YFJG",zj);
										 zfgrid.set("SL",sl);
									}
									zflist.add(zfgrid);
								}
							}

						}
						//创建模块信息

						if(moduleMap.containsKey(bm)) {
							BO mkBo=new BO();
						RowMap rowMap=	moduleMap.get(bm);
						mkBo.set("SSXT", rowMap.getString("SSXT"));
						mkBo.set("PMKSSDL", rowMap.getString("PMKSSDL"));
						mkBo.set("MKMC", rowMap.getString("MKMC"));
						mkBo.set("MKBM", rowMap.getString("MKBM"));
						mkBo.set("GG", rowMap.getString("GG"));
						mkBo.set("DW", rowMap.getString("DW"));
						mkBo.set("CLF", json.getString("CLF"));
						mkBo.set("RGF", json.getString("RGF"));
						mkBo.set("WXWG", json.getString("WXWG"));
						mkBo.set("CGF", json.getString("CGF"));
						mkBo.set("ZFWZYF", json.getString("ZFWZYF"));
						mkBo.set("MKDJ", json.getString("DJ"));
						mkBo.set("ZZFBL", rowMap.getString("ZZFBL"));
						String dj=json.getString("DJ");
						String clf=json.getString("CLF");
						String rgf=json.getString("RGF");
						String bl=rowMap.getString("ZZFBL").isEmpty()?"0":rowMap.getString("ZZFBL");
						BigDecimal djnum = new BigDecimal(dj);
						BigDecimal clnum = new BigDecimal(clf);
						BigDecimal rgnum = new BigDecimal(rgf);
						BigDecimal blnum = new BigDecimal(bl);
						BigDecimal num = new BigDecimal("100");
						BigDecimal num1 = new BigDecimal("1");
						BigDecimal zzf=	clnum.add(rgnum).divide(num1.subtract(blnum.divide(num)), 4).multiply(blnum.divide(num));
						mkBo.set("ZZF",zzf );
						mkBo.set("DJZZF", zzf.add(djnum));
						mkBo.set("SITRUE", rowMap.getString("SITRUE"));
						mkBo.set("SLJSGZ", rowMap.getString("SLJSGZ"));
						mkBo.set("MKSL", json.getString("SL"));
						mkBo.set("GLPID", pid);
						mkBo.set("XGBJDH", wfno);
						mkMaps.put(bm, mkBo);
						}

						wlMaps.put(bm, lists);
						zfMaps.put(bm, zflist);
						csMaps.put(bm, cslists);
					} catch (Exception e) {
						throw e;
					}finally {
						try {
							workbookwl.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
			  			 try {
							workbook.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}

				}
				//创建对于的物料清单信息
				System.out.println("csMaps:"+csMaps.toString());
				if(mkMaps.size()>0) {
					for (String bm : mkMaps.keySet()) {
						BO data=mkMaps.get(bm);
						ProcessInstance mpid=SDK.getProcessAPI().createBOProcessInstance("obj_338e4eb2168e4219811be2ab72f0e255", userContext.getUID(), "");
						SDK.getBOAPI().create("BO_EU_CPQ_MODULE_RECORD", data, mpid.getId(), userContext.getUID());
                        if(wlMaps.containsKey(bm)) {
                        	List<BO> list=wlMaps.get(bm);
                        	if(list.size()>0) {
                        		SDK.getBOAPI().create("BO_EU_CPQ_MODULE_RECORD_GRID", list, mpid.getId(), userContext.getUID());
                        	}
                        }
                        if(zfMaps.containsKey(bm)) {
                        	List<BO> list=zfMaps.get(bm);
                        	if(list.size()>0) {
                        		SDK.getBOAPI().create("BO_EU_CPQ_MODULE_RECORD_STRAIGHT_HAIR", list, mpid.getId(), userContext.getUID());
                        	}
                        }
                        if(csMaps.containsKey(bm)) {
                        	List<BO> list=csMaps.get(bm);
                        	if(list.size()>0) {
                        		SDK.getBOAPI().create("BO_EU_CPQ_MODULE_RECORD_PARAMETERS", list, mpid.getId(), userContext.getUID());
                        	}
                        }
					}
				}


		} catch (Exception e) {
			throw  e;
		}
	}









}
